package com.xu.consumer.service.Impl;

import com.xu.consumer.service.QRcodeService;
import com.xu.utils.Base64Util;
import com.xu.utils.QRCodeUtil;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @author 徐老板
 * @date 2020/3/31   13:40
 */
@Service
public class QRcodeServiceImpl implements QRcodeService {
    @Override
    public void createQrcode(String msg, HttpServletResponse response) {
        //校验参数合法性
        if (msg!=null && msg.length()>0){
            //参数合理的话进行解码
            String str = Base64Util.base64UrlDec(msg);
            //然后生成二维码
            BufferedImage image = QRCodeUtil.createColor(str, 500);
            try {
                ImageIO.write(image, "png", response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
